﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Threading.Tasks;
using Abp.AspNetCore.Mvc.Authorization;
using FwProject.Controllers;
using Microsoft.AspNetCore.Mvc;
using QRCoder;
using FwProject.Restaurant;
using Newtonsoft.Json;
using Abp.AutoMapper;
using System.Net;
using System.Linq;
using FwProject.Restaurant.Dto;
using FwProject.PrePaymentInfo.Dto;
using FwProject.Settlement;
using System.Collections.Generic;
using FwProject.Settlement.Dto;
using FwProject.Distribution;
using FwProject.Distribution.Dto;
using FwProject.ManagerConfig;
using FwProject.Comm;
using Abp.Web.Models;
using Abp.Dependency;
using FwProject.PrePaymentInfo;

namespace FwProject.Web.Controllers
{
    [AbpMvcAuthorize]
    public class PrintController : FwProjectControllerBase
    {
        private readonly IReceiptInfoAppService _receiptInfoAppService;
        private readonly ISettlementAccountAppService _settlementAccountAppService;
        private readonly ISettlementInfoAppService _settlementInfoAppService;
        private readonly IDistributionDetailsAppService _distributionDetailsAppService;
        private readonly IConfigInfoAppService _configInfoAppService;
        private readonly IPrePaymentInfoAppService _prePaymentInfoAppService;

        private  List<PrintHelpData> _printJSON;
        public PrintController(IReceiptInfoAppService receiptInfoAppService, ISettlementAccountAppService settlementAccountAppService,
            ISettlementInfoAppService settlementInfoAppService, IDistributionDetailsAppService distributionDetailsAppService,
            IConfigInfoAppService configInfoAppService, List<PrintHelpData> printJSON, IPrePaymentInfoAppService prePaymentInfoAppService)
        {
            _printJSON = printJSON;
            _receiptInfoAppService = receiptInfoAppService;
            _settlementAccountAppService = settlementAccountAppService;
            _settlementInfoAppService = settlementInfoAppService;
            _distributionDetailsAppService = distributionDetailsAppService;
            _configInfoAppService = configInfoAppService;
            _prePaymentInfoAppService = prePaymentInfoAppService;
        }
        /// <summary>
        /// 根据餐厅编号得到生成餐厅数据源数据
        /// </summary>
        /// <returns>The index.</returns>
        /// <param name="id">Identifier.</param>
        public ActionResult Index(string html)
        {
            ViewBag.html = html;
            return View();
        }
        /// <summary>
        /// 打印收款单据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<IActionResult> ReceiptPrint(Guid id)
        {
            var receipt = _receiptInfoAppService.GetById(id);

            var account = await _settlementAccountAppService.GetByIdAsync(receipt.CapitalAccountId);
            ViewBag.Account = account.Name;
            var settlement = await _settlementInfoAppService.GetSettlementInfoByReceiptInfoIdAsync(id);

            Tuple<ReceiptInfoDto, List<SettlementInfoDto>> tuple = new Tuple<ReceiptInfoDto, List<SettlementInfoDto>>(receipt, settlement);

            return View(tuple);
        }

        public async Task<IActionResult> PrintDistribution(string ids)
        {
            var config = await _configInfoAppService.GetListAsync();
            ViewBag.Config = config;
            string[] stringguids = ids.Split('|');

            List<Guid> guids = new List<Guid>();
            foreach (var item in stringguids)
            {
                guids.Add(Guid.Parse(item));
            }
            var model1 = _settlementInfoAppService.GetListById(guids);
            return View(model1);
        }
        /// <summary>
        /// 打印对账单
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<IActionResult> PrintDz(string ids)
        {
            var config = await _configInfoAppService.GetListAsync();
            ViewBag.Config = config;
            string[] stringguids = ids.Split('|');

            List<Guid> guids = new List<Guid>();
            foreach (var item in stringguids)
            {
                guids.Add(Guid.Parse(item));
            }
            var model = _settlementInfoAppService.GetListById(guids);
            return View(model);
        }
        /// <summary>
        /// 预收款单打印
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<IActionResult> PrePaymentPrint(Guid id)
        {
            var config = await _configInfoAppService.GetListAsync();
            ViewBag.Config = config;
            var model = await _prePaymentInfoAppService.GetPrePaymentPrintModelAsync(id);
            
            return View(model);
        }
        /// <summary>
        /// 退还预收款打印
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<IActionResult> ReturnPrePaymnetPrint(Guid id)
        {
            var config = await _configInfoAppService.GetListAsync();
            ViewBag.Config = config;
            var model = await _prePaymentInfoAppService.GetPrePaymentPrintModelAsync(id);

            return View(model);
        }

        public IActionResult GetPrintJson()
        {
         
            return Json(_printJSON);
        }
        [WrapResult(false, false)]
        public IActionResult SetPrintJson(List<PrintHelpData> printHelpDatas)
        {
            _printJSON = printHelpDatas;
            return Json("success");
        }
    }
}
